(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.lookup.loanListController", loanListController);
	loanListController.$inject = [ "$scope", "$uibModalInstance", "dialogsModule", "gridService", "layoutConfig", "validatorConfig", "authService", "ajaxServiceFactory" ];
	function loanListController($scope, $uibModalInstance, dialogsModule, gridService, layoutConfig, validatorConfig, authService, ajaxServiceFactory) {
		var ctrl = this;
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;

		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["ok"] || dialogsModule.btnCallback["ok"](ctrl.selectedRows) !== false) {
				$uibModalInstance.close();
			}
		};

		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};

		ctrl.init = function() {
			var ajaxServices = ajaxServiceFactory.buildAjaxService({
				getLookupTxnOptions : {
					url : "do/cls/dataList/lookupTxnOptions",
					method : "post"
				}
			});
			var _params = {};
			_params.prodType = "PPF";
			ajaxServices.getLookupTxnOptions(_params).then(function(response) {
				var data = response.data;
				ctrl.prodCodeOptions = data.prodCode;
				ctrl.loanCcyOptions = data.currency;
			});
			
			ctrl.loanCcyReadonly = false;
			if(ctrl.dialogsModule.params.loanCcyReadonly == true){
				ctrl.loanCcyReadonly = true;
			}
			
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = "vm.searchCriteria.list";
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			if(ctrl.dialogsModule.params.multiSelect == "true"){
				ctrl.gridOptions.multiSelect = true;
			}
			if (ctrl.dialogsModule.params.loanNo) {
				ctrl.searchCriteria.loanNo = ctrl.dialogsModule.params.loanNo;// get
			} else {
				ctrl.searchCriteria.loanNo = "";
			}
			if (ctrl.dialogsModule.params.lockFlag) {
				ctrl.searchCriteria.lockFlag = ctrl.dialogsModule.params.lockFlag;
			}
			if (ctrl.dialogsModule.params.loanStatusList) {
				ctrl.searchCriteria.loanStatusList = ctrl.dialogsModule.params.loanStatusList;
			}
			if (ctrl.dialogsModule.params.loanCcy) {
				ctrl.searchCriteria.loanCcy = ctrl.dialogsModule.params.loanCcy;
			} else {
				delete ctrl.searchCriteria.loanCcy;
			}
			if (ctrl.dialogsModule.params.prodType) {
				ctrl.searchCriteria.prodType = ctrl.dialogsModule.params.prodType;
			} else {
				delete ctrl.searchCriteria.prodType;
			}
			if (ctrl.dialogsModule.params.custCode) {
				ctrl.searchCriteria.custCode = ctrl.dialogsModule.params.custCode;
			} else {
				delete ctrl.searchCriteria.custCode;
			}
			if (ctrl.dialogsModule.params.obligorCode) {
				ctrl.searchCriteria.obligorCode = ctrl.dialogsModule.params.obligorCode;
			} else {
				delete ctrl.searchCriteria.obligorCode;
			}
			if (ctrl.dialogsModule.params.loanStatus) {
				ctrl.searchCriteria.loanStatus = ctrl.dialogsModule.params.loanStatus;
			} else {
				delete ctrl.searchCriteria.loanStatus;
			}
			if (ctrl.dialogsModule.params.lockFlag) {
				ctrl.searchCriteria.lockFlag = ctrl.dialogsModule.params.lockFlag;
			} else {
				delete ctrl.searchCriteria.lockFlag;
			}
			ctrl.searchCriteria.filterLoanIds = [];
			if (ctrl.dialogsModule.params.filterLoanIds) {
				if(angular.isArray(ctrl.dialogsModule.params.filterLoanIds)){
					ctrl.searchCriteria.filterLoanIds = ctrl.dialogsModule.params.filterLoanIds;// get params from parent page
				}
			}
			if (ctrl.dialogsModule.params.effectiveDateCheckWithBizDate) {
				ctrl.searchCriteria.effectiveDateCheckWithBizDate = true;
			} else {
				ctrl.searchCriteria.effectiveDateCheckWithBizDate = false;
			}
			ctrl.search();
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : "";
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : "";
			ctrl.search();
		}
	}
})(angular);
